package com.weme.common.adapter

import android.content.Context
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import com.weme.common.R
import com.weme.common.dialog.GiftDialog
import com.weme.common.dialog.GiftDialogCallback
import com.weme.common.util.loadImage
import com.weme.common.util.setVisible
import dev.kratos.api.user_server.v1.GiftInfo

class GiftChildAdapter(private val onItemClick: (Pair<View, GiftInfo>) -> Unit) : BaseQuickAdapter<GiftDialog.NewGiftInfo, QuickViewHolder>() {


var giftBean : GiftInfo? = null
    override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: GiftDialog.NewGiftInfo?) {
        item?.let {
            if (holder.layoutPosition == 0){
                giftBean = it.bean
            }
            holder.getView<View>(R.id.vBack).setVisible(it.isSelect)
            val imGift = holder.getView<ImageView>(R.id.imGift)
            imGift.loadImage(it.bean.giftIcon)
            holder.setText(R.id.tvGiftName, it.bean.giftName)
            holder.setText(R.id.tvGift, it.bean.giftPrice.toString())
           val calView = holder.getView<ConstraintLayout>(R.id.calView)
            calView.setOnClickListener {v->
                onItemClick.invoke(Pair(holder.getView<View>(R.id.vBack),it.bean))
            }
        }
    }

    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
        super.onViewAttachedToWindow(holder)
        if(holder.layoutPosition == 0){
            giftBean?.let {
                onItemClick.invoke(Pair((holder as QuickViewHolder).getView(R.id.vBack),it))
            }

        }


    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
        return QuickViewHolder(R.layout.item_gift_chind, parent)
    }


}
